package com.example.liuhb.broadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

/**
 * Created by liu.hb on 2018/6/20.
 */
// 继承BroadcastReceivre基类（动态注册）
public class mBroadcastReceiver extends BroadcastReceiver {
    // 复写onReceive()方法
    // 接收到广播后，则自动调用该方法
    @Override
    public void onReceive(Context context, Intent intent) {
        //写入接收广播后的操作
        System.out.println("------接收到广播----------");
        ConnectivityManager connectivityManager =
                (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo =connectivityManager.getActiveNetworkInfo();
        if(networkInfo!=null && networkInfo.isAvailable()){
            Toast.makeText(context,"恢复网络",Toast.LENGTH_SHORT).show();
            System.out.println("------恢复网络----------");
        }else{
            Toast.makeText(context,"网络断开",Toast.LENGTH_SHORT).show();
            System.out.println("------网络断开----------");
        }
        //不要添加过多的逻辑或者进行其他任何的耗时操作，不允许开启线程
    }
}
